package org.example.panel.datapanel;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import org.example.trans.*;
import org.example.inout.*;
import org.example.operation.*;

/**
 * @author lenovo
 */

public class InDataPanel extends JPanel {
    public InDataPanel(){
        //组件准备
        JButton inData = new JButton("导入txt");
        JButton inDataExcel = new JButton("导入excel");
        JButton inDataTest = new JButton("导入测试");
        Box box = Box.createHorizontalBox();
        //注册监视器
        inData.addActionListener((ActionEvent e)->{
            JFileChooser fileChooser = new JFileChooser();
            if(fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION){
                File file = fileChooser.getSelectedFile();
                String[] paths = file.getAbsolutePath().split("[.]");
                if(!"txt".equals(paths[paths.length-1]) && !"TXT".equals(paths[paths.length-1])){
                    JOptionPane.showMessageDialog(null,
                            "文件必须为.txt文件", null, JOptionPane.WARNING_MESSAGE);
                }else{
                    ArrayList<ArrayList<String>> arrayList = InOut.read(file.getAbsolutePath());
                    if(arrayList == null){
                        JOptionPane.showMessageDialog(null,
                                "找不到文件", null, JOptionPane.WARNING_MESSAGE);
                    }else{
                        int formatValue = DataOperation.formatValue(arrayList);
                        if (formatValue != -1) {
                            JOptionPane.showMessageDialog(null,
                                    "第" + (formatValue + 1) + "行与第一行的列数不一致",
                                    null, JOptionPane.WARNING_MESSAGE);
                        }else if(DataOperation.addData(TransScore.transScoreReadCourse(arrayList),
                                    TransScore.transScoreReadScore(arrayList)) == -1) {
                            JOptionPane.showMessageDialog(null,
                                    "学生数超出800000上限",null,
                                    JOptionPane.WARNING_MESSAGE);
                        }else{
                            JOptionPane.showMessageDialog(null,
                                    "导入成功", null, JOptionPane.WARNING_MESSAGE);
                        }
                    }
                }
            }
        });
        inDataExcel.addActionListener((ActionEvent e)->{
            JFileChooser fileChooser = new JFileChooser();
            if(fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION){
                File file = fileChooser.getSelectedFile();
                String[] paths = file.getAbsolutePath().split("[.]");
                if(!"xlsx".equals(paths[paths.length-1]) &&
                        !"XLSX".equals(paths[paths.length-1])){
                    JOptionPane.showMessageDialog(null,
                            "文件必须为.xlsx文件", null, JOptionPane.WARNING_MESSAGE);
                }else{
                    ArrayList<ArrayList<String>> arrayList =
                            InOutExcel.readExcel(file.getAbsolutePath());
                    if(arrayList == null) {
                        JOptionPane.showMessageDialog(null,
                                "找不到文件", null, JOptionPane.WARNING_MESSAGE);
                    }else{
                        int formatValue = DataOperation.formatValue(arrayList);
                        if (formatValue != -1) {
                            JOptionPane.showMessageDialog(null,
                                    "第" + (formatValue + 1) + "行与第一行的列数不一致\n" +
                                            "或第" + (formatValue + 1) + "行有非分隔符空格" ,
                                    null, JOptionPane.WARNING_MESSAGE);
                        }else if(DataOperation.addData(TransScore.transScoreReadCourse(arrayList),
                                    TransScore.transScoreReadScore(arrayList)) == -1){
                            JOptionPane.showMessageDialog(null,
                                    "学生数超出800000上限", null,
                                    JOptionPane.WARNING_MESSAGE);
                        }else{
                            JOptionPane.showMessageDialog(null,
                                    "导入成功", null, JOptionPane.WARNING_MESSAGE);
                        }
                    }
                }
            }
        });
        inDataTest.addActionListener((ActionEvent e)->{
            if(DataOperation.addTest() == -1){
                JOptionPane.showMessageDialog(null,
                        "学生数超出800000上限", null, JOptionPane.WARNING_MESSAGE);
            }else{
                JOptionPane.showMessageDialog(null,
                        "导入成功", null, JOptionPane.WARNING_MESSAGE);
            }
        });
        //添加组件
        box.add(inData);
        box.add(Box.createHorizontalStrut(5));
        box.add(inDataExcel);
        box.add(Box.createHorizontalStrut(5));
        box.add(inDataTest);
        add(box);
    }
}
